package com.hongning.railway.ui.main;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.text.TextUtils;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.AppViewModelFactory;
import com.hongning.railway.app.Constant;
import com.hongning.railway.databinding.ActivityMainBinding;
import com.hongning.railway.entity.reqbean.ReqAddScoreBean;
import com.hongning.railway.entity.LoginEntity;
import com.hongning.railway.ui.main.fragments.college.CollegeFragment;
import com.hongning.railway.ui.main.fragments.dynamic.DynamicFragment;
import com.hongning.railway.ui.main.fragments.home.HomeFragment;
import com.hongning.railway.ui.main.fragments.Minefragment;
import com.hongning.railway.util.Apputils;
import com.hongning.railway.ui.main.mine.PartyMemberInfoActivity;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.AppManager;
import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

public class MainActivity extends BaseActivity<ActivityMainBinding, BaseViewModel> {
    private List<Fragment> mFragments;
    private long exitTime = 0;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        initFragments();

        initBottomTab();

        initFragment();
        initMessager();
    }

    private void initBottomTab() {
        NavigationController navigationController = binding.pagerBottomTab.material()
                .addItem(R.mipmap.icon_home_normal, "资讯",getResources().getColor(R.color.textred))
                .addItem(R.mipmap.icon_dynamic_normal, "动态",getResources().getColor(R.color.textred))
                .addItem(R.mipmap.icon_college_normal, "学院",getResources().getColor(R.color.textred))
                .addItem(R.mipmap.icon_mine_normal, "我的",getResources().getColor(R.color.textred))
                .build();
        //底部按钮的点击事件监听
        navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.frameLayout, mFragments.get(index));
                transaction.commitAllowingStateLoss();
            }

            @Override
            public void onRepeat(int index) {
            }
        });
        navigationController.setSelect(0);
    }

    private void initFragment(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frameLayout, mFragments.get(0));
        transaction.commitAllowingStateLoss();
    }

    private void initFragments() {
        if (mFragments == null) {
            mFragments = new ArrayList<>();
            mFragments.add(HomeFragment.newInstance());
            mFragments.add(DynamicFragment.newInstance());
            mFragments.add(CollegeFragment.newInstance());
           LoginEntity loginEntity= Apputils.getLoginInfo();
           if (loginEntity!=null&&loginEntity.getGroups()!=null&&!loginEntity.getGroups().isEmpty())
           {

               mFragments.add(Minefragment.newInstance());
           }else {
               mFragments.add(PartyMemberInfoActivity.newInstance());
           }
        }
    }

    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            ToastUtils.showLong("再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            RichText.recycle();
            AppManager.getAppManager().AppExit();
            System.exit(0);

        }
    }

    private void initMessager(){
        //接收添加積分請求
        /**
         * 001001001001    登陆平台
         * 001001001002    新闻阅读
         * 001001001003    视听学习
         * 001001001004    上传心得体会
         */
        Messenger.getDefault().register(this, Constant.ADD_SCORE, ReqAddScoreBean.class,new BindingConsumer<ReqAddScoreBean>() {
            @Override
            public void call(ReqAddScoreBean reqAddScore) {
                if (TextUtils.isEmpty(reqAddScore.getStaffId())){
                    reqAddScore.setStaffId(Apputils.getStaffid());
                }
                AppViewModelFactory.getInstance(getApplication()).getmRepository().
                        addScore(reqAddScore)
                        .compose(RxUtils.schedulersTransformer())
                        .compose(bindToLifecycle())
                        .subscribe(new Consumer() {
                            @Override
                            public void accept(Object o) throws Exception {

                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                throwable.printStackTrace();
                            }
                        });
            }
        });
    }
}
